<template>
  <el-card>
    <div slot="header">
      <el-form :model="filterParams" ref="filter-form">
        <el-form-item label="账号或昵称" prop="phone">
          <el-input
            v-model="filterParams.key"
            placeholder="账号或昵称"
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleQuery" :disabled="loading"
            >查询</el-button
          >
        </el-form-item>
      </el-form>
    </div>
    <el-table :data="users" style="width: 100%" v-loading="loading">
      <el-table-column prop="uID" label="用户ID" width="100"> </el-table-column>
      <el-table-column prop="name" label="头像" width="80">
        <template slot-scope="scope">
          <img
            width="30px"
            :src="
              scope.row.uPhoto ||
              'https://cube.elemecdn.com/9/c2/f0ee8a3c7c9638a54940382568c9dpng.png'
            "
          />
        </template>
      </el-table-column>
      <el-table-column prop="name" label="用户名" width="120">
      </el-table-column>
      <el-table-column prop="uCreateTime" label="注册时间" width="120">
      </el-table-column>
      <el-table-column prop="address" label="操作">
        <template slot-scope="scope">
          <el-button type="text" @click="handleSelectRole(scope.row)"
            >分配角色</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <el-dialog title="分配角色" :visible.sync="dialogVisible" width="50%">
      <el-select v-model="roleIdList" multiple placeholder="请选择">
        <el-option
          v-for="item in roles"
          :key="item.id"
          :label="item.name"
          :value="item.id"
        >
        </el-option>
      </el-select>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="handleAllocRole">确 定</el-button>
      </span>
    </el-dialog>
  </el-card>
</template>

<script>
import { getUserPages } from '@/services/system/user'
import { getRoles } from '@/services/system/role'
import { Message } from 'element-ui'
export default {
  name: 'UserList',
  data() {
    return {
      users: [],
      filterParams: {
        page: 1,
        key: '',
      },
      loading: true,
      dialogVisible: false,
      roles: [],
      roleIdList: [],
      currentUser: null, // 分配角色的当前用户
    }
  },

  created() {
    this.loadUsers()
  },

  methods: {
    async loadUsers() {
      this.loading = true
      const { data } = await getUserPages(this.filterParams)
      if (data === undefined) {
        Message('系统异常')
      } else {
      }
      this.users = data.response.data
      this.loading = false
    },

    handleQuery() {
      this.loadUsers()
    },
    handleAllocRole() {},
    async handleSelectRole(role) {
      this.currentUser = role
      // 加载角色列表
      const { data } = await getRoles()
      this.roles = data.data

      // const {
      //   data: { data: userRoles },
      // } = await getUserRoles(this.currentUser.id)
      // this.roleIdList = userRoles.map((item) => item.id)

      // 展示对话框
      this.dialogVisible = true
    },
  },
}
</script>

<style lang="scss" scoped></style>
